package com.mocchira.dq2011.sp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.mocchira.dq2011.sp.SlidePuzzleAnalyzer.Result;

public class SlidePuzzleAnswerReader {
    private static SlidePuzzleAnalyzer.Result[] results = null;
    public static final SlidePuzzleAnalyzer.Result[] getResult(File file) {
        if (results == null) {
            if (file == null || !file.canRead())
                return null;
            BufferedReader reader = null;
            List<SlidePuzzleAnalyzer.Result> listResult = new ArrayList<SlidePuzzleAnalyzer.Result>();
            try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                String line;
                while ((line =reader.readLine()) != null) {
                    Result ret = new Result();
                    if (line.length() > 0) {
                        ret.addFromAnswerString(line);
                    }
                    listResult.add(ret);
                }
                SlidePuzzleAnalyzer.Result[] tmp = new Result[1];
                results = listResult.toArray(tmp);
            } catch (IOException iex) {
                System.err.println(iex.getMessage());
                return null;
            } finally {
                if (reader != null) try {reader.close();} catch (Exception ex){;}
            }
        }
        return results;
    }
}
